package com.onpositive.mediawiki.propertyparser;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;

import storage.v1.ParserV2;

import com.onpositive.semantic.model.api.meta.IMeta;
import com.onpositive.units.AbstractPreprocessor;
import com.onpositive.units.AbstractValueParser;
import com.onpositive.units.ParsedScalar;
import com.onpositive.units.ParsedValue;

public class ScalarParser  extends AbstractValueParser {
	
//	private static HashMap<String, Integer> numberStringMap ;  
	
	public ScalarParser(){
		super.id = ParsedScalar.ID ;
	}

	@Override
	public ParsedValue parse(String value, IMeta meta, String comment) {
		
		if( value == null )
			return null ;

		String arr[] = value.split(" ") ;
		StringBuilder bld = new StringBuilder() ;
		boolean gotpoint = false ;
		boolean startedNumber = false ;
		int i = 0 ;
		for(  ; i < arr.length ; i++ )
		{
			String s = arr[i] ;
			int l_ = s.length();
			boolean goodString = true ;
			for( int j = 0 ; j < l_ ; j++ )
			{
				char ch = s.charAt(j) ;
				if( Character.isDigit(ch) ){
					bld.append( ch ) ;
					startedNumber = true ;
					continue ;
				}				
				else if( ch == '.' && !gotpoint && startedNumber )
				{
					bld.append( ch ) ;
					gotpoint = true ;
					continue ;
				}
				else if( ch == ',' && startedNumber){
					bld.append( ch ) ;
					continue ;
				}
				if( !startedNumber )
					continue ;
				
				goodString = false ;
				break ;
			}
			if(!goodString)
				break ;
		}
		
		String scalarString = bld.toString() ;		
		double doubleValue = 0.0 ;
		try {
			doubleValue = NumberFormat.getInstance(Locale.US).parse( scalarString ).doubleValue();
			return new ParsedScalar( doubleValue, getDefaultComment(comment) ) ;
		
		} catch (ParseException e) {		
			
		}
		
		Integer iValue = readNumber( value ) ;
		if ( iValue != null )
			return new ParsedScalar( iValue, getDefaultComment(comment) ) ;		

		return null ;
	}

	private Integer readNumber(String str ) {
		
		String value = str.trim().toLowerCase() ;
		
		if( value.startsWith("zero") )		 return  0 ;
		if( value.startsWith("one") )		 return  1 ;
		if( value.startsWith("two") )		 return  2 ;
		if( value.startsWith("three") )		 return  3 ;
		if( value.startsWith("four") )		 return  4 ;
		if( value.startsWith("five") )		 return  5 ;
		if( value.startsWith("six") )		 return  6 ;
		if( value.startsWith("seven") )		 return  7 ;
		if( value.startsWith("eight") )		 return  8 ;
		if( value.startsWith("nine") )		 return  9 ;
		if( value.startsWith("ten") )		 return 10 ;
		if( value.startsWith("eleven") )	 return 11 ;
		if( value.startsWith("twelve") )	 return 12 ;
		if( value.startsWith("thirteen") )	 return 13 ;
		if( value.startsWith("fourrteen") )	 return 14 ;
		if( value.startsWith("fifteen") )	 return 15 ;
		if( value.startsWith("sixteen") )	 return 16 ;
		if( value.startsWith("seventeen") )	 return 17 ;
		if( value.startsWith("eightteen") )	 return 18 ;
		if( value.startsWith("nineteen") )	 return 19 ;
		if( value.startsWith("twenty") )	 return 20 ;		
		
		return null;
	}

	@Override
	public AbstractPreprocessor getPreprocessor() {

		return null ;
	}
	
//	static{
//		
//		numberStringMap = new HashMap<String, Integer>() ;
//		numberStringMap.put("zero"		 ,  0 ) ;
//		numberStringMap.put("one"		 ,  1 ) ;
//		numberStringMap.put("two"		 ,  2 ) ;
//		numberStringMap.put("three"		 ,  3 ) ;
//		numberStringMap.put("four"		 ,  4 ) ;
//		numberStringMap.put("five"		 ,  5 ) ;
//		numberStringMap.put("six"		 ,  6 ) ;
//		numberStringMap.put("seven"		 ,  7 ) ;
//		numberStringMap.put("eight"		 ,  8 ) ;
//		numberStringMap.put("nine"		 ,  9 ) ;
//		numberStringMap.put("ten"		 , 10 ) ;
//		numberStringMap.put("eleven"	 , 11 ) ;
//		numberStringMap.put("twelve"	 , 12 ) ;
//		numberStringMap.put("thirteen"	 , 13 ) ;
//		numberStringMap.put("fourrteen"	 , 14 ) ;
//		numberStringMap.put("fifteen"	 , 15 ) ;
//		numberStringMap.put("sixteen"	 , 16 ) ;
//		numberStringMap.put("seventeen"	 , 17 ) ;
//		numberStringMap.put("eightteen"	 , 18 ) ;
//		numberStringMap.put("nineteen"	 , 19 ) ;
//		numberStringMap.put("twenty"	 , 20 ) ;		
//
//	}

}